package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.monitor.model.Image;
import cn.sccl.dirms.monitor.model.Script;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图标数据
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/script")
public class ScriptController {


    /**
     * 根据名称获取脚本
     * @param name
     */
    @RequestMapping(value = "/getByName/{name}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> getImageById(@PathVariable String name) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<>();
        params.put("output", "extend");
        Map<String, Object> filter = new HashMap<>();
        filter.put("name", name);
        params.put("filter", filter);
        Map<String, Object> result = zabbix.call("script.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }

    /**
     * 执行脚本
     * @param hostid 主机id
     * @param scriptid 脚本id
     */
    @RequestMapping(value = "/excute/{hostid}/{scriptid}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String,Object> excuteScript(@PathVariable String hostid, @PathVariable String scriptid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<>();
        params.put("hostid", hostid);
        params.put("scriptid", scriptid);
        Map<String, Object> result = zabbix.call("script.execute", params);
        return (Map<String, Object>) result.get("result");
    }

    /**
     * 执行脚本
     * @param script 脚本对象
     */
    @RequestMapping(value = "/excute2", method = {RequestMethod.POST,RequestMethod.GET})
    public Script excuteScript2(@RequestBody Script script) {

        String cmdStr = script.getName() + " " + script.getIpaddress();
        if (script.getName().equals("ping")) {
            cmdStr = script.getName() + " -c 5 " + script.getIpaddress();
        } else if (script.getName().equals("traceroute")) {
            cmdStr = script.getName() + " -m 5 " + script.getIpaddress();
        }
        System.out.println(cmdStr);
        try {
            Process p = Runtime.getRuntime().exec(cmdStr);
            InputStream is = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            p.waitFor();
            if (p.exitValue() != 0) {
                //说明命令执行失败
                //可以进入到错误处理步骤中
                return script;
            }
            String s = null;
            StringBuffer resultStr = new StringBuffer();
            while ((s = reader.readLine()) != null) {
                resultStr.append(s+"\n");
            }
            script.setResult(resultStr.toString());
            p.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return script;
    }
}
